/*
 * Copyright 2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.tigris.jasmincc.implementation;

import java.util.*;

import org.tigris.jasmincc.*;
import org.tigris.jasmincc.framework.*;
import org.tigris.mappedlexer.validator.VerifyHub;

/**
 * 
 * @author Sam Huang
 * @deprecated
 */
@Deprecated
public class TempRegularExpressionProduction extends AbstractProduction
		implements StorableProduction {

	protected Map<String, Object> cache = null;

	protected NamingManager namingManager = null;

	protected String[] tokens = new String[2];

	protected int type;

	private String key;

	public void parse() throws Exception {
		namingManager = (NamingManager) cache.get("namingManager");

		tk = lexer.nexttok(false);
		if (tk.equals(SYM_QUOTATION)) {
			tokens[0] = lexer.skipQutedString();
		} else if (tk.equals(SYM_LEFT_TRIANGLE)) {
			tk = lexer.nexttok(false);
			if (tk.equals(SYM_SHARP)) {
				/*
				 * ( "<" [ [ "#" ] identifier() ":" ]
				 * complex_regular_expression_choices() ">" )
				 */
				tk = lexer.nexttok(false);
			}
			if (tk.equals(JAVACC_EOF)) {
				lexer.print(JAVACC_EOF);
			} else if (VerifyHub.IDENTIFIER.validate(tk)) {
				getToken();
			} else {
				lexer.backward(tk);
				tk = lexer.skipTo('<');
				System.out.println(tk);
			}
		}
	}

	private void getToken() throws Exception {
		this.key = tk;
		key = namingManager.updateRef(key);

		String nexttk = lexer.nexttok(false, SYM_COLON, SYM_RIGHT_TRIANGLE);
		if (nexttk.equals(SYM_COLON)) {
			tk = lexer.skipTo('<');

			tk = org.tigris.jasmincc.utils.StringServices.

			decorate(tk);
			tk = change(tk);

			tokens[0] = key;
			tokens[1] = tk;
		} else {
			tokens[0] = tk;
			tokens[1] = null;
		}
	}

	/**
	 * @return the list
	 */
	public List<String> getTokens() {
		// return this.tokens;
		return null;
	}

	/**
	 * @param cache
	 *            the cache to set
	 */
	public void setCache(Map<String, Object> cache) {
		this.cache = cache;
	}

	protected String change(String tk) {
		StringBuffer mirror = new StringBuffer();

		for (char chr : tk.toCharArray()) {
			mirror.append(chr);
			if (chr == '<') {
				if (namingManager.getInstanceName().length() > 0) {
					mirror.append(namingManager.getInstanceName()).append(
							SYM_UNDER_SCORE);
				}
			}
		}
		return mirror.toString();
	}

}
